package daily

import (
	"fmt"
	"strconv"
	"time"

	"github.com/xuri/excelize"
)

// 变量
var (
	// colnamemap 列名映射
	colnamemap []string
)

// 初始化
func init() {
	colnamemap = append(colnamemap, []string{
		"A", "B", "C", "D", "E",
		"F", "G", "H", "I", "J",
		"K", "L", "M", "N", "O",
		"P", "Q", "R", "S", "T",
		"U", "V", "W", "X", "Y",
		"Z",
	}...)
}

// GetColName 获取列名称 A B C ...
func GetColName(index int) string {
	if index <= 0 {
		return ""
	}
	value := index - 1
	if value == 0 {
		return "A"
	}
	ret := ""
	for true {
		i := value % 26
		ret = colnamemap[i] + ret
		value = value/26 - 1
		if value < 0 {
			break
		}
	}
	return ret
}

// UpdateMatch 更新成交额累计页面
func UpdateMatch(xlsx *excelize.File, matchIndex, productIndex, zhongIndex string, tTradeDate time.Time, work int, last int) {
	fmt.Println("更新表格 ...")

	// 获取商品市场行数
	rows := xlsx.GetRows(productIndex)
	productStart := 2
	productStop := len(rows)

	// 列名
	colname := GetColName(work)

	// 日期
	cell := colname + "1"
	xlsx.SetCellValue(matchIndex, cell, tTradeDate)
	style, errstyle := xlsx.NewStyle(`{"number_format":14}`)
	if errstyle == nil {
		xlsx.SetCellStyle(matchIndex, cell, cell, style)
	}

	// 复制商品市场页面数据
	for i := productStart; i <= productStop; i++ {
		// 取值
		vcell := "H" + strconv.Itoa(i)
		value := xlsx.GetCellValue(productIndex, vcell)

		// 赋值
		cell = colname + strconv.Itoa(i)
		fValue, errv := strconv.ParseFloat(value, 64)
		if errv == nil {
			xlsx.SetCellValue(matchIndex, cell, fValue)
		} else {
			xlsx.SetCellValue(matchIndex, cell, value)
		}
		style, errstyle := xlsx.NewStyle(`{"number_format":2}`)
		if errstyle == nil {
			xlsx.SetCellStyle(matchIndex, cell, cell, style)
		}
	}

	// 复制金融期货数据
	vcell := "B56"
	value := xlsx.GetCellValue(zhongIndex, vcell)
	cell = colname + strconv.Itoa(productStop+1)
	dValue, errv := strconv.ParseFloat(value, 64)
	if errv == nil {
		xlsx.SetCellValue(matchIndex, cell, dValue)
	} else {
		xlsx.SetCellValue(matchIndex, cell, value)
	}
	style, errstyle = xlsx.NewStyle(`{"number_format":2}`)
	if errstyle == nil {
		xlsx.SetCellStyle(matchIndex, cell, cell, style)
	}
}

// UpdatePosi 更新持仓额累计页面
func UpdatePosi(xlsx *excelize.File, posiIndex, productIndex, zhongIndex string, tTradeDate time.Time, work int, last int) {
	fmt.Println("更新表格 ...")

	// 获取商品市场行数
	rows := xlsx.GetRows(productIndex)
	productStart := 2
	productStop := len(rows)

	// 列名
	colname := GetColName(work)

	// 日期
	cell := colname + "1"
	xlsx.SetCellValue(posiIndex, cell, tTradeDate)
	style, errstyle := xlsx.NewStyle(`{"number_format":14}`)
	if errstyle == nil {
		xlsx.SetCellStyle(posiIndex, cell, cell, style)
	}

	// 复制商品市场数据
	for i := productStart; i <= productStop; i++ {
		// 取值
		vcell := "J" + strconv.Itoa(i)
		value := xlsx.GetCellValue(productIndex, vcell)

		// 赋值
		cell = colname + strconv.Itoa(i)
		dValue, errv := strconv.ParseFloat(value, 64)
		if errv == nil {
			xlsx.SetCellValue(posiIndex, cell, dValue)
		} else {
			xlsx.SetCellValue(posiIndex, cell, value)
		}
		style, errstyle = xlsx.NewStyle(`{"number_format":2}`)
		if errstyle == nil {
			xlsx.SetCellStyle(posiIndex, cell, cell, style)
		}
	}

	// 复制金融期货数据
	vcell := "B55"
	value := xlsx.GetCellValue(zhongIndex, vcell)
	cell = colname + strconv.Itoa(productStop+1)
	dValue, errv := strconv.ParseFloat(value, 64)
	if errv == nil {
		xlsx.SetCellValue(posiIndex, cell, dValue)
	} else {
		xlsx.SetCellValue(posiIndex, cell, value)
	}
	style, errstyle = xlsx.NewStyle(`{"number_format":2}`)
	if errstyle == nil {
		xlsx.SetCellStyle(posiIndex, cell, cell, style)
	}
}
